<?php

/*

    MMetaverse - Open Metaverse Web Manager

    Copyright (c) 2009 Mary Jane Soft - Marlon J. Manrique

    http://www.maryjanesoft.com
    http://www.marlonj.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    Boston, MA  02111-1307  USA

    $Id$

*/

Yii::import('application.models.mmetaverse.MMetaverseModel');
Yii::import('application.models.mmetaverse.MAvatarFormModel');

/**
 * Avatar controller
 */
class AvatarController extends CController {

    /**
     * Returns the filter configurations.
     *
     * @return array a list of filter configurations.
     */
    public function filters()
    {
        // Enable access control
        return array(
            'accessControl',
        );
    }

    /**
     * Set Access Rules
     */
    public function accessRules() {
        return array
        (
            // Admin can create avatars
            array('allow','actions' => array('create'),
                    'users' => array('admin')),
            // Users can create avatars 
            array('deny','actions' => array('create'),
                    'users' => array('@')),
            // Guest can do nothing 
            array('deny','actions' => array('list','info','create'),
                    'users' => array('?')),
        );
    }

    /**
     * Show list of avatars 
     */
    public function actionList() {
        try {
            // Get metaverse and user manager
            $metaverse = MMetaverseModel::getMetaverse();
            $userManager = $metaverse->getUserManager();

            // Get users
            $users = $userManager->getUsers();
        } catch (Exception $e) {
            // If any problem return an empty list of users 
            $users = array();
        }

        $this->render('MAvatarListView', array('users'=>$users));
    }

    /**
     * Show avatar information 
     */
    function actionShow() {
        // Check if the uuid is specified
        if(isset($_REQUEST['uuid'])) {
            // Get metaverse and region manager
            $metaverse = MMetaverseModel::getMetaverse();
            $userManager = $metaverse->getUserManager();
            $regionManager = $metaverse->getRegionManager();
            
            // Create UUID
            $uuid = new MUUID($_REQUEST['uuid']);

            // Get user info and home region info 
            $user = $userManager->getUser($uuid);

            // Try to get the region if not found set like null 
            try {
                $homeRegion = $regionManager->getRegion($user->getHomeRegionID());
            } catch (Exception $e) {
                $homeRegion = null;
            }

            // Render user info
            $this->render('MAvatarShowView',
                array('user'=>$user,'homeRegion'=>$homeRegion));
        }
    }

    /**
     * Create new user
     */
    public function actionCreate() {
        // Create Avatar Form 
        $avatarForm = new MAvatarFormModel();

        // Check if submit data 
        if(isset($_POST['MAvatarFormModel'])) {
            // Load Attributes
            $avatarForm->attributes = $_POST['MAvatarFormModel'];

            // Validate the information 
            if($avatarForm->validate())
            {
                // Create the user
                try {
                    // Get user manager
                    $metaverse = MMetaverseModel::getMetaverse();
                    $userManager = $metaverse->getUserManager();

                    // Create a user with a random uuid and set form information
                    $uuid = MUUID::random();
                    $user = new MUser($uuid,
                        $avatarForm->firstName,$avatarForm->lastName);
                    $user->setPassword($avatarForm->password);

                    // Try to create the user 
                    $userManager->createUser($user);

                    // Show the information of the user
                    $this->redirect(array('show',
                        'uuid'=>$uuid->getUnHyphenated()));
                    
                } catch (Exception $e) {
                    // Show exception message
                    $avatarForm->addError('firstName',$e->getMessage());
                }
            }
        }

        // Render the avatar form 
        $this->render('MAvatarCreateView',array('avatarForm'=>$avatarForm));
    }
}

?>
